package cn.gson.springboot.model.service.afterservice;

import cn.gson.springboot.model.dao.afterdao.CulogDao;
import cn.gson.springboot.model.mapper.aftermapper.CulogMapper;
import cn.gson.springboot.model.pjos.Culog;
import cn.gson.springboot.model.pjos.Emp;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CulogService {

    @Autowired
    CulogDao dao;
    @Autowired
    CulogMapper mapper;

    //分页查询日志表
    public List<Culog> selectCulogPage(Integer userName, Integer fatherDeptId, Integer logType, String title){
        return mapper.logPage(userName, fatherDeptId, logType, title);
    }

    //新增日志表
    public void addLog(Culog log){
        if(log.getCulStatus() == null || log.getCulStatus() == ""){
            log.setCulStatus("未点评");
        }
        if(log.getCulTime() == null){
            Timestamp time = new Timestamp(new Date().getTime());
            log.setCulTime(time);
        }
        log.setEmpByEmployeesId(log.getEmpByEmployeesId());
        log.setCulDelete(1);
        dao.save(log);
    }

    //逻辑删除
    public void delete(String deleteId){
        JSONObject obj = (JSONObject) JSONObject.parse(deleteId);
        List list = obj.getObject("deleteList",List.class);
        //转化成JSONArray
        JSONObject object = null;
        Culog log = null;
        JSONArray json = JSONArray.parseArray(String.valueOf(list));
        for (int i=0;i<json.size();i++){
            object = (JSONObject) json.get(i);//得到对象
            Integer culId = object.getInteger("culId");
            log = dao.findById(culId).get();
            log.setCulDelete(0);
            dao.save(log);
        }
    }

    //回显日志表
    public Culog selectByCulId(Integer culId){
        return mapper.logByCulId(culId);
    }
}
